#include "header.hpp"
using namespace std;

// Singleton Pattern
// Create in heap
/* Core: construcotr is private
 * (1)Provide static member function to create object, return pointer of singleton object
 * (2)Free resource by static member function
 * (3)delete copy and assign function
*/

class Singleton1{
private:
    int x;
    int y;
    static Singleton1* _pInstance;

    Singleton1(int x, int y) : x(x), y(y){}
    ~Singleton1(){}

public:
    Singleton1(const Singleton1 &) = delete;
    Singleton1& operator=(const Singleton1 &) = delete;

    //public interface
    static Singleton1* getInstance(){
        if(_pInstance == nullptr){
            _pInstance = new Singleton1(0, 0);
        }
        return _pInstance;
    }

    static void destroy(){
        if(_pInstance){
            delete _pInstance;
            _pInstance = nullptr;
        }
    }

    void set(int x, int y){
        this->x = x;
        this->y = y;
    }

    void print(){
        cout << "(" << x << ", " << y << ")" << endl;
    }
};

//Initialize the static variable
Singleton1* Singleton1::_pInstance = nullptr;

void test12(){
    Singleton1::getInstance()->print();

    Singleton1::getInstance()->set(2, 3);
    Singleton1::getInstance()->print();

    Singleton1::getInstance()->set(4, 5);
    Singleton1::getInstance()->print();

    Singleton1::destroy();
    Singleton1::destroy();
}
